package list;

public class MyLinkList implements ILinkList{
    static class ListNode{
    public int val;
    public ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}
    public ListNode head;
    //创建链表
    public ListNode create() {
        ListNode node1 = new ListNode(20);
        ListNode node2 = new ListNode(15);
        ListNode node3 = new ListNode(25);
        ListNode node4 = new ListNode(33);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
        return head;
    }
    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }
        //找到最后一个节点
        ListNode cur = finPreListNode(head);
        cur.next = node;
    }

    private ListNode finPreListNode(ListNode cur) {
        while (cur.next != null) {
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public void addIndex(int index, int data) {
        //判断下标是否合法
        if(index <0 || index > size()){
            throw new IndexException("下标"+index+"不合法");
        }
        ListNode node = new ListNode(data);
        //节点为空
        if(head == null) {
            head = node;
            return;
        }
        //在0位置插入
        if(index == 0) {
            addFirst(data);
            return;
        }
        //在末尾插入
        if(index == size()) {
            addLast(data);
            return;
        }
        //中间位置插入
        ListNode cur = findIndexPre(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode findIndexPre(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    /*是否存在key值*/
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }else {
                cur = cur.next;
            }
        }
        return false;
    }

    /*删除第一个元素*/
    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        //头节点是要删除的值
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = finPreKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode finPreKey(int key) {
        ListNode cur = new ListNode(key);
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        //删除头节点
        while (head.val == key) {
            head = head.next;
        }
        //要删除的前一个节点
        ListNode pre = head;
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = pre.next;
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        head = null;
    }
}
